<template>
  <div>
    <router-link to="/book/AddBook" v-if="role==2"><el-button>添加</el-button></router-link>
    <el-table
      :data="bookList"
      :stripe="true"
      style="width: 100%">
      <el-table-column
        prop="bookId"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="bookName"
        label="书名">
      </el-table-column>
      <el-table-column
        prop="bookPrice"
        label="原价">
      </el-table-column>
      <el-table-column
        prop="dangprice"
        label="押金">
      </el-table-column>
      <el-table-column
        prop="borrowPrice"
        label="租金(/天)">
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button  size="mini" v-if="role==2" @click="deleteBook(scope.row.bookId)">删除</el-button>
          <el-button size="mini" v-if="role==1" @click="putCart(scope.row.bookId)">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="选择天数" :visible.sync="dialogFormVisible">
      <el-form  label-width="100px">

        <el-form-item label="天数">
          <el-input-number v-model="num" controls-position="right" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.prevent="test">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <router-view/>
  </div>
</template>

<script>
    export default {
        name: "bookList",
      data(){
          return {
            bookList:[],
            role:sessionStorage.getItem("role"),
            num:1,
            bid:1,
            dialogFormVisible:false,
          }
      },
      created(){
        this.selectAll();
      },
      methods:{
        selectAll(){
          //发出ajax请求请求后端代码
          let url="/book/selectAll";
          console.log(this.$route.query.cid);
          if(this.$route.query.cid){
            url+="?cid="+this.$route.query.cid;
          }
          this.axios.get(url).then((resp)=>{
            this.bookList=resp.data;
          });
        },
        deleteBook(id){
          this.axios.delete("/book/delete/"+id).then((resp)=>{
            this.$message.success("删除成功！");
            this.selectAll();
          });
        },
        putCart(bid){
         this.bid=bid;
         this.dialogFormVisible=true;
        },
        test(){
          this.$confirm("是否确认放入购物车","保存提示").then(()=>{
            this.axios.get("/cart/putCart/"+this.bid+"/"+this.num).then((resp)=>{
              this.$message.success("保存成功！");
              this.dialogFormVisible=false;
              this.num=1;
            });
          }).catch(()=>{});
        }
      }
    }
</script>

<style scoped>

</style>
